<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@include file="_include.jsp" %>

<html>
<%--路径 /forum/section/{sectionId}/page/{currPage}--%>
<head>
    <script src="${pageContext.request.contextPath}/resources/js/jquery-2.1.1.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>${map.section.name}</title>
</head>
<body>
<h1>${map.section.name}</h1>

<a href="/forum/section/${map.section.sectionId}/article">发贴</a><br/><br/>

<c:forEach items="${map.topicList}" var="topic">
    <a href="/forum/section/${map.section.sectionId}/topic/${topic.topicId}">${topic.title}----------${topic.viewNum}/${topic.replyNum}</a><br/>
</c:forEach>

<%--分页--%>
<div>
    <p id="pages"></p>
</div>
<script type="text/javascript">
    function check(totalPage, currPage) {
        totalPage = totalPage == 0 ? 1 : totalPage;
        var maxShowPage = 5;
        var startPage;
        var endPage;
        if (totalPage <= maxShowPage) {
            startPage = 1;
            endPage = totalPage;
        } else {
            startPage = currPage - (maxShowPage - 1) / 2;
            startPage = startPage < 1 ? 1 : startPage;
            endPage = startPage + (maxShowPage - 1);
            if (endPage > totalPage) {
                endPage = totalPage;
                startPage = endPage - (maxShowPage - 1);
            }

            startPage = Math.floor(startPage);
            endPage = Math.floor(endPage);
        }

        console.log('currPage=' + currPage);
        console.log('startPage=' + startPage);
        console.log('endPage=' + endPage);
        return {
            startPage: startPage,
            endPage: endPage
        }
    }

    $('document').ready(function () {
        console.log('aa');
        var page = check(${map.maxPage}, ${map.currPage});
        var ptn = '【 <a href="/forum/section/${map.section.sectionId}/page/{index1}">{index2}</a> 】';
        var ptn2 = '【 {index2} 】';
        var text = '';
        if (${map.currPage >1}) {
            text += '<a href="/forum/section/${map.section.sectionId}/page/${map.currPage -1}">上一页</a>';
        } else {
            text += '上一页'
        }
        if (${map.currPage <map.maxPage}) {
            text += '<a href="/forum/section/${map.section.sectionId}/page/${map.currPage +1}">下一页</a>';
        } else {
            text += '下一页'
        }

        for (var idx = page.startPage; idx <= page.endPage; idx++) {
            if (idx ==${map.currPage}) {
                text += ptn2.replace('{index2}', idx);
                continue;
            }
            var link = ptn.replace('{index1}', idx);
            link = link.replace('{index2}', idx);
            text += link;
        }


        if (text) {
            $('#pages').html(text);
        }
    });
</script>
</body>
</html>
